package com.example.learnshadow;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import java.io.File;

/*
https://cloud.tencent.com/developer/article/2252752
使用 DexClassLoader 动态加载字节码文件

探索Android开源框架 - 10. 插件化原理
https://zhuanlan.zhihu.com/p/450350883

Android插件化换肤
https://blog.csdn.net/qq_25956141/article/details/122051417
 */

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PluginManagerImpl.getInstance().setContext(this);
        checkPermission();

    }

    public boolean checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
            }, 1);

        }
        return false;
    }

    public void onClick(@NonNull View view) {
        int id = view.getId();
        if (R.id.tv_load_plugin == id) {
            Log.e("==", "点击了加载插件");
            // 先将 plugina 打包好的 plugina-debug.apk 放入到设备sdcard文件夹里，后的 apk文件夹是自己创建的存放apk文件的
            File file = new File(Environment.getExternalStorageDirectory() + "/apk", "plugina-debug.apk");
//            File file = new File(Environment.getExternalStorageDirectory(), "plugina-debug.apk");
            PluginManagerImpl.getInstance().loadPath(file.getAbsolutePath());
            return;

        }

        if (R.id.tv_open == id) {
            Log.e("==", "点击了打开插件页面");

            Intent intent = new Intent(this, PluginContainerActivity.class);
            startActivity(intent);
            return;
        }


    }
}